import logging
from datetime import datetime
import time
import random
import json
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

class LotteryScraper:
    """
    中国体育彩票双色球历史数据爬虫类
    使用Selenium模拟浏览器访问
    """
    
    def __init__(self):
        """
        构造函数
        初始化Selenium WebDriver
        """
        # 配置Chrome选项
        chrome_options = Options()
        chrome_options.add_argument('--headless')  # 无头模式
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--window-size=1920,1080')
        chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36')
        
        # 初始化WebDriver
        self.driver = webdriver.Chrome(options=chrome_options)
        self.wait = WebDriverWait(self.driver, 10)
        logging.info("爬虫初始化完成")

    def fetch_lottery_data(self, page=1, size=30, start_date=None, end_date=None):
        """
        获取双色球开奖数据
        :param page: 页码
        :param size: 每页数量
        :param start_date: 开始日期
        :param end_date: 结束日期
        :return: 开奖数据列表
        """
        try:
            # 访问网页
            url = f"https://www.cwl.gov.cn/ygkj/wqkjgg/ssq/kjgg/"
            self.driver.get(url)
            
            # 等待页面加载
            time.sleep(3)
            
            # 等待数据表格加载
            table = self.wait.until(
                EC.presence_of_element_located((By.CLASS_NAME, "historylist"))
            )
            
            # 获取所有行
            rows = table.find_elements(By.TAG_NAME, "tr")
            
            lottery_list = []
            for row in rows[1:]:  # 跳过表头
                try:
                    # 获取单元格数据
                    cells = row.find_elements(By.TAG_NAME, "td")
                    if len(cells) >= 7:
                        # 解析红球和蓝球
                        red_balls = cells[1].text.split()
                        blue_ball = cells[2].text
                        
                        lottery_info = {
                            '期号': cells[0].text,
                            '开奖日期': cells[3].text,
                            '红球': red_balls,
                            '蓝球': blue_ball,
                            '奖池金额': cells[4].text.replace(',', ''),
                            '一等奖注数': cells[5].text,
                            '一等奖奖金': cells[6].text.replace(',', '')
                        }
                        lottery_list.append(lottery_info)
                        logging.info(f"成功解析第 {lottery_info['期号']} 期开奖数据")
                except Exception as e:
                    logging.warning(f"解析行数据时出错: {e}")
                    continue
            
            return lottery_list
            
        except TimeoutException:
            logging.error("页面加载超时")
            return []
        except Exception as e:
            logging.error(f"获取开奖数据失败: {e}")
            return []

    def fetch_data_by_year(self, year):
        """
        获取指定年份的数据
        :param year: 年份
        :return: 开奖数据列表
        """
        all_data = []
        page = 1
        
        while True:
            logging.info(f"正在获取 {year} 年第 {page} 页数据...")
            data = self.fetch_lottery_data(
                page=page,
                size=30,
                start_date=f"{year}0101",
                end_date=f"{year}1231"
            )
            
            if not data:
                break
                
            all_data.extend(data)
            
            # 检查是否已经获取到该年份的所有数据
            if any(item['开奖日期'].startswith(str(year)) for item in data):
                break
                
            page += 1
            if page > 100:  # 设置最大页数限制
                break
        
        return all_data

    def save_to_mysql_file(self, data, filename):
        """
        将数据保存为MySQL导入文件
        :param data: 要保存的数据
        :param filename: 文件名
        """
        try:
            with open(filename, 'w', encoding='utf-8') as f:
                # 写入建表语句
                f.write("""CREATE TABLE IF NOT EXISTS lottery_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    draw_number VARCHAR(20) NOT NULL COMMENT '期号',
    draw_date DATE NOT NULL COMMENT '开奖日期',
    red_ball_1 INT NOT NULL COMMENT '红球1',
    red_ball_2 INT NOT NULL COMMENT '红球2',
    red_ball_3 INT NOT NULL COMMENT '红球3',
    red_ball_4 INT NOT NULL COMMENT '红球4',
    red_ball_5 INT NOT NULL COMMENT '红球5',
    red_ball_6 INT NOT NULL COMMENT '红球6',
    blue_ball INT NOT NULL COMMENT '蓝球',
    first_prize_count INT NOT NULL COMMENT '一等奖注数',
    first_prize_amount DECIMAL(20,2) NOT NULL COMMENT '一等奖奖金',
    pool_amount DECIMAL(20,2) NOT NULL COMMENT '奖池金额',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='双色球历史数据';\n\n""")
                
                # 写入数据插入语句
                for item in data:
                    red_balls = [int(x) for x in item['红球']]
                    sql = f"""INSERT INTO lottery_data (
    draw_number, draw_date, 
    red_ball_1, red_ball_2, red_ball_3, red_ball_4, red_ball_5, red_ball_6,
    blue_ball, first_prize_count, first_prize_amount, pool_amount
) VALUES (
    '{item['期号']}', '{item['开奖日期']}',
    {red_balls[0]}, {red_balls[1]}, {red_balls[2]}, {red_balls[3]}, {red_balls[4]}, {red_balls[5]},
    {item['蓝球']}, {item['一等奖注数']}, {item['一等奖奖金']}, {item['奖池金额']}
);\n"""
                    f.write(sql)
            
            logging.info(f"数据已保存到MySQL文件: {filename}")
        except IOError as e:
            logging.error(f"保存文件时出错: {e}")

    def __del__(self):
        """
        析构函数
        关闭浏览器
        """
        try:
            self.driver.quit()
        except:
            pass

def main():
    """
    主函数
    测试获取2025年的数据
    """
    logging.info("开始运行双色球数据爬虫...")
    
    # 创建爬虫实例
    scraper = LotteryScraper()
    
    try:
        # 获取当前时间作为文件名的一部分
        current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
        
        # 获取2025年的数据
        year = 2025
        logging.info(f"\n开始获取 {year} 年的数据...")
        
        # 获取该年份的数据
        year_data = scraper.fetch_data_by_year(year)
        
        if year_data:
            # 按日期排序
            year_data.sort(key=lambda x: x['开奖日期'], reverse=True)
            
            # 保存为MySQL文件
            mysql_filename = f'lottery_data_{year}_{current_time}.sql'
            scraper.save_to_mysql_file(year_data, mysql_filename)
            
            # 打印数据统计信息
            print("\n数据统计信息:")
            print(f"总期数: {len(year_data)}")
            print(f"最早日期: {year_data[-1]['开奖日期']}")
            print(f"最新日期: {year_data[0]['开奖日期']}")
            print(f"\nMySQL文件已生成: {mysql_filename}")
            
            # 打印详细数据
            print("\n详细数据:")
            print("-" * 80)
            for item in year_data:
                print(f"期号: {item['期号']}")
                print(f"开奖日期: {item['开奖日期']}")
                print(f"红球: {' '.join(item['红球'])}")
                print(f"蓝球: {item['蓝球']}")
                print(f"一等奖注数: {item['一等奖注数']}")
                print(f"一等奖奖金: {item['一等奖奖金']}")
                print(f"奖池金额: {item['奖池金额']}")
                print("-" * 80)
            
            logging.info(f"成功获取 {year} 年数据，共 {len(year_data)} 期")
        else:
            logging.warning(f"未能获取到 {year} 年的任何数据")
    
    finally:
        # 确保浏览器被关闭
        scraper.__del__()
    
    logging.info("爬虫程序运行完成")

if __name__ == '__main__':
    main() 